Wikify Evernote

Purpose

Set up

a better script found online: https://gist.github.com/smargh/7668654

on run {input, parameters}
	
	tell application id "com.evernote.Evernote"
		set Evernote_Selection to selection
		if Evernote_Selection is {} then display dialog "Please select a note."
		repeat with i from 1 to the count of Evernote_Selection
			--get appropriate note data from current note
			set noteURL to note link of item i of Evernote_Selection
			set noteName to title of item i of Evernote_Selection
			set noteNB to name of notebook of item i of Evernote_Selection
			set noteHTML to HTML content of item i of Evernote_Selection
			--generate the hyperlink
			set html_ref to "<a href=\"" & noteURL & "\">" & noteName & "</a>"
			
			--get the selected text from the clipboard
			set newTitle to input
			--create the new note, with the hyperlink back
			set newNote to create note title newTitle with html html_ref notebook noteNB
			
			--synchronize to assign server data to new note
			repeat until isSynchronizing is false
			end repeat
			synchronize
			repeat until isSynchronizing is false
			end repeat
			
			--get appropriate data of the new note
			set newURL to note link of newNote
			set newName to title of newNote
			set newhtml_ref to "<a href=\"" & newURL & "\">" & newName & "</a>"
			
			--replace the selected text with a hyperlink
			set newHTML to my replaceString(noteHTML, newTitle, newhtml_ref)
			set HTML content of item i of Evernote_Selection to newHTML
			
			--synchronize again to finalize everything
			repeat until isSynchronizing is false
			end repeat
			synchronize
			repeat until isSynchronizing is false
			end repeat
			
		end repeat
	end tell
	
end run

(* HANDLERS *)

on replaceString(theText, oldString, newString)
	-- ljr (http://applescript.bratis-lover.net/library/string/)
	local ASTID, theText, oldString, newString, lst
	set ASTID to AppleScript's text item delimiters
	try
		considering case
			set AppleScript's text item delimiters to oldString
			set lst to every text item of theText
			set AppleScript's text item delimiters to newString
			set theText to lst as string
		end considering
		set AppleScript's text item delimiters to ASTID
		return theText
	on error eMsg number eNum
		set AppleScript's text item delimiters to ASTID
		error "Can't replaceString: " & eMsg number eNum
	end try
end replaceString

Operation

References

Homepage
Comments

Hide Comments